local mt = ac.buff['隐身']

mt.cover_type = 1
mt.cover_max = 1

mt.buff = true
mt.remove_when_attack = false
mt.remove_when_spell = false
mt.trg_attack = nil
mt.trg_spell = nil

function mt:on_add()
	self.owner:add_restriction '隐身'
	if self.remove_when_attack then
		self.trg_attack = self.owner:event '单位-开始攻击' (function()
			self:remove()
		end)
	end
	if self.remove_when_spell then
		self.trg_spell = self.owner:event '技能-施法开始' (function()
			self:remove()
		end)
	end
end

function mt:on_remove()
	self.owner:remove_restriction '隐身'
	if self.trg_attack then
		self.trg_attack:remove()
	end
	if self.trg_spell then
		self.trg_spell:remove()
	end
end

function mt:on_cover(dest)
	--更改原来buff的持续时间
	if dest.time > self:get_remaining() then
		self:set_remaining(dest.time)
	end
	return false
end

local mt = ac.buff['渐隐']
mt.keep = true
mt.cover_type = 1
mt.cover_max = 1
-- 分6次，每0.2秒减少透明度10%
mt.default_alpha_data = {6, 0.2, 10}

function mt:on_add()
	if not self.alpha_data then
		self.alpha_data = self.default_alpha_data
	end
    if not self.origin_alpha then
		self.origin_alpha = self.owner:get_alpha()
    end
    self.alpha = self.origin_alpha
    self.timer = ac.timer(self.alpha_data[2]*1000, self.alpha_data[1], function()
        self.alpha = self.alpha - self.origin_alpha*self.alpha_data[3]*0.01
        self.owner:set_alpha(self.alpha)
	end)
end

function mt:on_remove()
    if self.timer then
        self.timer:remove()
    end
    self.owner:set_alpha(self.origin_alpha)
end

function mt:on_cover(new)
	self:set_remaining(new.time)
	return false
end